home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / perl / 5.10.0 / Pod / Simple / BlackBox.pm next >
Encoding:
Perl POD Document  |  2009-06-26  |  61.1 KB  |  1,924 lines

  1.  
  2. package Pod::Simple::BlackBox;
  3. #
  4. # "What's in the box?"  "Pain."
  5. #
  6. ###########################################################################
  7. #
  8. # This is where all the scary things happen: parsing lines into
  9. #  paragraphs; and then into directives, verbatims, and then also
  10. #  turning formatting sequences into treelets.
  11. #
  12. # Are you really sure you want to read this code?
  13. #
  14. #-----------------------------------------------------------------------------
  15. #
  16. # The basic work of this module Pod::Simple::BlackBox is doing the dirty work
  17. # of parsing Pod into treelets (generally one per non-verbatim paragraph), and
  18. # to call the proper callbacks on the treelets.
  19. #
  20. # Every node in a treelet is a ['name', {attrhash}, ...children...]
  21.  
  22. use integer; # vroom!
  23. use strict;
  24. use Carp ();
  25. BEGIN {
  26.   require Pod::Simple;
  27.   *DEBUG = \&Pod::Simple::DEBUG unless defined &DEBUG
  28. }
  29.  
  30. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  31.  
  32. sub parse_line { shift->parse_lines(@_) } # alias
  33.  
  34. # - - -  Turn back now!  Run away!  - - -
  35.  
  36. sub parse_lines {             # Usage: $parser->parse_lines(@lines)
  37.   # an undef means end-of-stream
  38.   my $self = shift;
  39.  
  40.   my $code_handler = $self->{'code_handler'};
  41.   my $cut_handler  = $self->{'cut_handler'};
  42.   $self->{'line_count'} ||= 0;
  43.  
  44.   my $scratch;
  45.  
  46.   DEBUG > 4 and 
  47.    print "# Parsing starting at line ", $self->{'line_count'}, ".\n";
  48.  
  49.   DEBUG > 5 and
  50.    print "#  About to parse lines: ",
  51.      join(' ', map defined($_) ? "[$_]" : "EOF", @_), "\n";
  52.  
  53.   my $paras = ($self->{'paras'} ||= []);
  54.    # paragraph buffer.  Because we need to defer processing of =over
  55.    # directives and verbatim paragraphs.  We call _ponder_paragraph_buffer
  56.    # to process this.
  57.   
  58.   $self->{'pod_para_count'} ||= 0;
  59.  
  60.   my $line;
  61.   foreach my $source_line (@_) {
  62.     if( $self->{'source_dead'} ) {
  63.       DEBUG > 4 and print "# Source is dead.\n";
  64.       last;
  65.     }
  66.  
  67.     unless( defined $source_line ) {
  68.       DEBUG > 4 and print "# Undef-line seen.\n";
  69.  
  70.       push @$paras, ['~end', {'start_line' => $self->{'line_count'}}];
  71.       push @$paras, $paras->[-1], $paras->[-1];
  72.        # So that it definitely fills the buffer.
  73.       $self->{'source_dead'} = 1;
  74.       $self->_ponder_paragraph_buffer;
  75.       next;
  76.     }
  77.  
  78.  
  79.     if( $self->{'line_count'}++ ) {
  80.       ($line = $source_line) =~ tr/\n\r//d;
  81.        # If we don't have two vars, we'll end up with that there
  82.        # tr/// modding the (potentially read-only) original source line!
  83.     
  84.     } else {
  85.       DEBUG > 2 and print "First line: [$source_line]\n";
  86.  
  87.       if( ($line = $source_line) =~ s/^\xEF\xBB\xBF//s ) {
  88.         DEBUG and print "UTF-8 BOM seen.  Faking a '=encode utf8'.\n";
  89.         $self->_handle_encoding_line( "=encode utf8" );
  90.         $line =~ tr/\n\r//d;
  91.         
  92.       } elsif( $line =~ s/^\xFE\xFF//s ) {
  93.         DEBUG and print "Big-endian UTF-16 BOM seen.  Aborting parsing.\n";
  94.         $self->scream(
  95.           $self->{'line_count'},
  96.           "UTF16-BE Byte Encoding Mark found; but Pod::Simple v$Pod::Simple::VERSION doesn't implement UTF16 yet."
  97.         );
  98.         splice @_;
  99.         push @_, undef;
  100.         next;
  101.  
  102.         # TODO: implement somehow?
  103.  
  104.       } elsif( $line =~ s/^\xFF\xFE//s ) {
  105.         DEBUG and print "Little-endian UTF-16 BOM seen.  Aborting parsing.\n";
  106.         $self->scream(
  107.           $self->{'line_count'},
  108.           "UTF16-LE Byte Encoding Mark found; but Pod::Simple v$Pod::Simple::VERSION doesn't implement UTF16 yet."
  109.         );
  110.         splice @_;
  111.         push @_, undef;
  112.         next;
  113.  
  114.         # TODO: implement somehow?
  115.         
  116.       } else {
  117.         DEBUG > 2 and print "First line is BOM-less.\n";
  118.         ($line = $source_line) =~ tr/\n\r//d;
  119.       }
  120.     }
  121.  
  122.  
  123.     DEBUG > 5 and print "# Parsing line: [$line]\n";
  124.  
  125.     if(!$self->{'in_pod'}) {
  126.       if($line =~ m/^=([a-zA-Z]+)/s) {
  127.         if($1 eq 'cut') {
  128.           $self->scream(
  129.             $self->{'line_count'},
  130.             "=cut found outside a pod block.  Skipping to next block."
  131.           );
  132.           
  133.           ## Before there were errata sections in the world, it was
  134.           ## least-pessimal to abort processing the file.  But now we can
  135.           ## just barrel on thru (but still not start a pod block).
  136.           #splice @_;
  137.           #push @_, undef;
  138.           
  139.           next;
  140.         } else {
  141.           $self->{'in_pod'} = $self->{'start_of_pod_block'}
  142.                             = $self->{'last_was_blank'}     = 1;
  143.           # And fall thru to the pod-mode block further down
  144.         }
  145.       } else {
  146.         DEBUG > 5 and print "# It's a code-line.\n";
  147.         $code_handler->(map $_, $line, $self->{'line_count'}, $self)
  148.          if $code_handler;
  149.         # Note: this may cause code to be processed out of order relative
  150.         #  to pods, but in order relative to cuts.
  151.         
  152.         # Note also that we haven't yet applied the transcoding to $line
  153.         #  by time we call $code_handler!
  154.  
  155.         if( $line =~ m/^#\s*line\s+(\d+)\s*(?:\s"([^"]+)")?\s*$/ ) {
  156.           # That RE is from perlsyn, section "Plain Old Comments (Not!)",
  157.           #$fname = $2 if defined $2;
  158.           #DEBUG > 1 and defined $2 and print "# Setting fname to \"$fname\"\n";
  159.           DEBUG > 1 and print "# Setting nextline to $1\n";
  160.           $self->{'line_count'} = $1 - 1;
  161.         }
  162.         
  163.         next;
  164.       }
  165.     }
  166.     
  167.     # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  168.     # Else we're in pod mode:
  169.  
  170.     # Apply any necessary transcoding:
  171.     $self->{'_transcoder'} && $self->{'_transcoder'}->($line);
  172.  
  173.     # HERE WE CATCH =encoding EARLY!
  174.     if( $line =~ m/^=encoding\s+\S+\s*$/s ) {
  175.       $line = $self->_handle_encoding_line( $line );
  176.     }
  177.  
  178.     if($line =~ m/^=cut/s) {
  179.       # here ends the pod block, and therefore the previous pod para
  180.       DEBUG > 1 and print "Noting =cut at line ${$self}{'line_count'}\n";
  181.       $self->{'in_pod'} = 0;
  182.       # ++$self->{'pod_para_count'};
  183.       $self->_ponder_paragraph_buffer();
  184.        # by now it's safe to consider the previous paragraph as done.
  185.       $cut_handler->(map $_, $line, $self->{'line_count'}, $self)
  186.        if $cut_handler;
  187.  
  188.       # TODO: add to docs: Note: this may cause cuts to be processed out
  189.       #  of order relative to pods, but in order relative to code.
  190.       
  191.     } elsif($line =~ m/^\s*$/s) {  # it's a blank line
  192.       if(!$self->{'start_of_pod_block'} and @$paras and $paras->[-1][0] eq '~Verbatim') {
  193.         DEBUG > 1 and print "Saving blank line at line ${$self}{'line_count'}\n";
  194.         push @{$paras->[-1]}, $line;
  195.       }  # otherwise it's not interesting
  196.       
  197.       if(!$self->{'start_of_pod_block'} and !$self->{'last_was_blank'}) {
  198.         DEBUG > 1 and print "Noting para ends with blank line at ${$self}{'line_count'}\n"; 
  199.       }
  200.       
  201.       $self->{'last_was_blank'} = 1;
  202.       
  203.     } elsif($self->{'last_was_blank'}) {  # A non-blank line starting a new para...
  204.       
  205.       if($line =~ m/^(=[a-zA-Z][a-zA-Z0-9]*)(?:\s+|$)(.*)/s) {
  206.         # THIS IS THE ONE PLACE WHERE WE CONSTRUCT NEW DIRECTIVE OBJECTS
  207.         my $new = [$1, {'start_line' => $self->{'line_count'}}, $2];
  208.          # Note that in "=head1 foo", the WS is lost.
  209.          # Example: ['=head1', {'start_line' => 123}, ' foo']
  210.         
  211.         ++$self->{'pod_para_count'};
  212.         
  213.         $self->_ponder_paragraph_buffer();
  214.          # by now it's safe to consider the previous paragraph as done.
  215.                 
  216.         push @$paras, $new; # the new incipient paragraph
  217.         DEBUG > 1 and print "Starting new ${$paras}[-1][0] para at line ${$self}{'line_count'}\n";
  218.         
  219.       } elsif($line =~ m/^\s/s) {
  220.  
  221.         if(!$self->{'start_of_pod_block'} and @$paras and $paras->[-1][0] eq '~Verbatim') {
  222.           DEBUG > 1 and print "Resuming verbatim para at line ${$self}{'line_count'}\n";
  223.           push @{$paras->[-1]}, $line;
  224.         } else {
  225.           ++$self->{'pod_para_count'};
  226.           $self->_ponder_paragraph_buffer();
  227.            # by now it's safe to consider the previous paragraph as done.
  228.           DEBUG > 1 and print "Starting verbatim para at line ${$self}{'line_count'}\n";
  229.           push @$paras, ['~Verbatim', {'start_line' => $self->{'line_count'}}, $line];
  230.         }
  231.       } else {
  232.         ++$self->{'pod_para_count'};
  233.         $self->_ponder_paragraph_buffer();
  234.          # by now it's safe to consider the previous paragraph as done.
  235.         push @$paras, ['~Para',  {'start_line' => $self->{'line_count'}}, $line];
  236.         DEBUG > 1 and print "Starting plain para at line ${$self}{'line_count'}\n";
  237.       }
  238.       $self->{'last_was_blank'} = $self->{'start_of_pod_block'} = 0;
  239.  
  240.     } else {
  241.       # It's a non-blank line /continuing/ the current para
  242.       if(@$paras) {
  243.         DEBUG > 2 and print "Line ${$self}{'line_count'} continues current paragraph\n";
  244.         push @{$paras->[-1]}, $line;
  245.       } else {
  246.         # Unexpected case!
  247.         die "Continuing a paragraph but \@\$paras is empty?";
  248.       }
  249.       $self->{'last_was_blank'} = $self->{'start_of_pod_block'} = 0;
  250.     }
  251.     
  252.   } # ends the big while loop
  253.  
  254.   DEBUG > 1 and print(pretty(@$paras), "\n");
  255.   return $self;
  256. }
  257.  
  258. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  259.  
  260. sub _handle_encoding_line {
  261.   my($self, $line) = @_;
  262.   
  263.   # The point of this routine is to set $self->{'_transcoder'} as indicated.
  264.  
  265.   return $line unless $line =~ m/^=encoding\s+(\S+)\s*$/s;
  266.   DEBUG > 1 and print "Found an encoding line \"=encoding $1\"\n";
  267.  
  268.   my $e    = $1;
  269.   my $orig = $e;
  270.   push @{ $self->{'encoding_command_reqs'} }, "=encoding $orig";
  271.  
  272.   my $enc_error;
  273.  
  274.   # Cf.   perldoc Encode   and   perldoc Encode::Supported
  275.  
  276.   require Pod::Simple::Transcode;
  277.  
  278.   if( $self->{'encoding'} ) {
  279.     my $norm_current = $self->{'encoding'};
  280.     my $norm_e = $e;
  281.     foreach my $that ($norm_current, $norm_e) {
  282.       $that =  lc($that);
  283.       $that =~ s/[-_]//g;
  284.     }
  285.     if($norm_current eq $norm_e) {
  286.       DEBUG > 1 and print "The '=encoding $orig' line is ",
  287.        "redundant.  ($norm_current eq $norm_e).  Ignoring.\n";
  288.       $enc_error = '';
  289.        # But that doesn't necessarily mean that the earlier one went okay
  290.     } else {
  291.       $enc_error = "Encoding is already set to " . $self->{'encoding'};
  292.       DEBUG > 1 and print $enc_error;
  293.     }
  294.   } elsif (
  295.     # OK, let's turn on the encoding
  296.     do {
  297.       DEBUG > 1 and print " Setting encoding to $e\n";
  298.       $self->{'encoding'} = $e;
  299.       1;
  300.     }
  301.     and $e eq 'HACKRAW'
  302.   ) {
  303.     DEBUG and print " Putting in HACKRAW (no-op) encoding mode.\n";
  304.  
  305.   } elsif( Pod::Simple::Transcode::->encoding_is_available($e) ) {
  306.  
  307.     die($enc_error = "WHAT? _transcoder is already set?!")
  308.      if $self->{'_transcoder'};   # should never happen
  309.     require Pod::Simple::Transcode;
  310.     $self->{'_transcoder'} = Pod::Simple::Transcode::->make_transcoder($e);
  311.     eval {
  312.       my @x = ('', "abc", "123");
  313.       $self->{'_transcoder'}->(@x);
  314.     };
  315.     $@ && die( $enc_error =
  316.       "Really unexpected error setting up encoding $e: $@\nAborting"
  317.     );
  318.  
  319.   } else {
  320.     my @supported = Pod::Simple::Transcode::->all_encodings;
  321.  
  322.     # Note unsupported, and complain
  323.     DEBUG and print " Encoding [$e] is unsupported.",
  324.       "\nSupporteds: @supported\n";
  325.     my $suggestion = '';
  326.  
  327.     # Look for a near match:
  328.     my $norm = lc($e);
  329.     $norm =~ tr[-_][]d;
  330.     my $n;
  331.     foreach my $enc (@supported) {
  332.       $n = lc($enc);
  333.       $n =~ tr[-_][]d;
  334.       next unless $n eq $norm;
  335.       $suggestion = "  (Maybe \"$e\" should be \"$enc\"?)";
  336.       last;
  337.     }
  338.     my $encmodver = Pod::Simple::Transcode::->encmodver;
  339.     $enc_error = join '' =>
  340.       "This document probably does not appear as it should, because its ",
  341.       "\"=encoding $e\" line calls for an unsupported encoding.",
  342.       $suggestion, "  [$encmodver\'s supported encodings are: @supported]"
  343.     ;
  344.  
  345.     $self->scream( $self->{'line_count'}, $enc_error );
  346.   }
  347.   push @{ $self->{'encoding_command_statuses'} }, $enc_error;
  348.  
  349.   return '=encoding ALREADYDONE';
  350. }
  351.  
  352. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  353.  
  354. sub _handle_encoding_second_level {
  355.   # By time this is called, the encoding (if well formed) will already
  356.   #  have been acted one.
  357.   my($self, $para) = @_;
  358.   my @x = @$para;
  359.   my $content = join ' ', splice @x, 2;
  360.   $content =~ s/^\s+//s;
  361.   $content =~ s/\s+$//s;
  362.  
  363.   DEBUG > 2 and print "Ogling encoding directive: =encoding $content\n";
  364.   
  365.   if($content eq 'ALREADYDONE') {
  366.     # It's already been handled.  Check for errors.
  367.     if(! $self->{'encoding_command_statuses'} ) {
  368.       DEBUG > 2 and print " CRAZY ERROR: It wasn't really handled?!\n";
  369.     } elsif( $self->{'encoding_command_statuses'}[-1] ) {
  370.       $self->whine( $para->[1]{'start_line'},
  371.         sprintf "Couldn't do %s: %s",
  372.           $self->{'encoding_command_reqs'  }[-1],
  373.           $self->{'encoding_command_statuses'}[-1],
  374.       );
  375.     } else {
  376.       DEBUG > 2 and print " (Yup, it was successfully handled already.)\n";
  377.     }
  378.     
  379.   } else {
  380.     # Otherwise it's a syntax error
  381.     $self->whine( $para->[1]{'start_line'},
  382.       "Invalid =encoding syntax: $content"
  383.     );
  384.   }
  385.   
  386.   return;
  387. }
  388.  
  389. #~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`
  390.  
  391. {
  392. my $m = -321;   # magic line number
  393.  
  394. sub _gen_errata {
  395.   my $self = $_[0];
  396.   # Return 0 or more fake-o paragraphs explaining the accumulated
  397.   #  errors on this document.
  398.  
  399.   return() unless $self->{'errata'} and keys %{$self->{'errata'}};
  400.  
  401.   my @out;
  402.   
  403.   foreach my $line (sort {$a <=> $b} keys %{$self->{'errata'}}) {
  404.     push @out,
  405.       ['=item', {'start_line' => $m}, "Around line $line:"],
  406.       map( ['~Para', {'start_line' => $m, '~cooked' => 1},
  407.         #['~Top', {'start_line' => $m},
  408.         $_
  409.         #]
  410.         ],
  411.         @{$self->{'errata'}{$line}}
  412.       )
  413.     ;
  414.   }
  415.   
  416.   # TODO: report of unknown entities? unrenderable characters?
  417.  
  418.   unshift @out,
  419.     ['=head1', {'start_line' => $m, 'errata' => 1}, 'POD ERRORS'],
  420.     ['~Para', {'start_line' => $m, '~cooked' => 1, 'errata' => 1},
  421.      "Hey! ",
  422.      ['B', {},
  423.       'The above document had some coding errors, which are explained below:'
  424.      ]
  425.     ],
  426.     ['=over',  {'start_line' => $m, 'errata' => 1}, ''],
  427.   ;
  428.  
  429.   push @out, 
  430.     ['=back',  {'start_line' => $m, 'errata' => 1}, ''],
  431.   ;
  432.  
  433.   DEBUG and print "\n<<\n", pretty(\@out), "\n>>\n\n";
  434.  
  435.   return @out;
  436. }
  437.  
  438. }
  439.  
  440. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  441.  
  442. ##############################################################################
  443. ##
  444. ##  stop reading now stop reading now stop reading now stop reading now stop
  445. ##
  446. ##                         HERE IT BECOMES REALLY SCARY
  447. ##
  448. ##  stop reading now stop reading now stop reading now stop reading now stop
  449. ##
  450. ##############################################################################
  451.  
  452. sub _ponder_paragraph_buffer {
  453.  
  454.   # Para-token types as found in the buffer.
  455.   #   ~Verbatim, ~Para, ~end, =head1..4, =for, =begin, =end,
  456.   #   =over, =back, =item
  457.   #   and the null =pod (to be complained about if over one line)
  458.   #
  459.   # "~data" paragraphs are something we generate at this level, depending on
  460.   # a currently open =over region
  461.  
  462.   # Events fired:  Begin and end for:
  463.   #                   directivename (like head1 .. head4), item, extend,
  464.   #                   for (from =begin...=end, =for),
  465.   #                   over-bullet, over-number, over-text, over-block,
  466.   #                   item-bullet, item-number, item-text,
  467.   #                   Document,
  468.   #                   Data, Para, Verbatim
  469.   #                   B, C, longdirname (TODO -- wha?), etc. for all directives
  470.   # 
  471.  
  472.   my $self = $_[0];
  473.   my $paras;
  474.   return unless @{$paras = $self->{'paras'}};
  475.   my $curr_open = ($self->{'curr_open'} ||= []);
  476.  
  477.   my $scratch;
  478.  
  479.   DEBUG > 10 and print "# Paragraph buffer: <<", pretty($paras), ">>\n";
  480.  
  481.   # We have something in our buffer.  So apparently the document has started.
  482.   unless($self->{'doc_has_started'}) {
  483.     $self->{'doc_has_started'} = 1;
  484.     
  485.     my $starting_contentless;
  486.     $starting_contentless =
  487.      (
  488.        !@$curr_open  
  489.        and @$paras and ! grep $_->[0] ne '~end', @$paras
  490.         # i.e., if the paras is all ~ends
  491.      )
  492.     ;
  493.     DEBUG and print "# Starting ", 
  494.       $starting_contentless ? 'contentless' : 'contentful',
  495.       " document\n"
  496.     ;
  497.     
  498.     $self->_handle_element_start(
  499.       ($scratch = 'Document'),
  500.       {
  501.         'start_line' => $paras->[0][1]{'start_line'},
  502.         $starting_contentless ? ( 'contentless' => 1 ) : (),
  503.       },
  504.     );
  505.   }
  506.  
  507.   my($para, $para_type);
  508.   while(@$paras) {
  509.     last if @$paras == 1 and
  510.       ( $paras->[0][0] eq '=over' or $paras->[0][0] eq '~Verbatim'
  511.         or $paras->[0][0] eq '=item' )
  512.     ;
  513.     # Those're the three kinds of paragraphs that require lookahead.
  514.     #   Actually, an "=item Foo" inside an <over type=text> region
  515.     #   and any =item inside an <over type=block> region (rare)
  516.     #   don't require any lookahead, but all others (bullets
  517.     #   and numbers) do.
  518.  
  519. # TODO: winge about many kinds of directives in non-resolving =for regions?
  520. # TODO: many?  like what?  =head1 etc?
  521.  
  522.     $para = shift @$paras;
  523.     $para_type = $para->[0];
  524.  
  525.     DEBUG > 1 and print "Pondering a $para_type paragraph, given the stack: (",
  526.       $self->_dump_curr_open(), ")\n";
  527.     
  528.     if($para_type eq '=for') {
  529.       next if $self->_ponder_for($para,$curr_open,$paras);
  530.  
  531.     } elsif($para_type eq '=begin') {
  532.       next if $self->_ponder_begin($para,$curr_open,$paras);
  533.  
  534.     } elsif($para_type eq '=end') {
  535.       next if $self->_ponder_end($para,$curr_open,$paras);
  536.  
  537.     } elsif($para_type eq '~end') { # The virtual end-document signal
  538.       next if $self->_ponder_doc_end($para,$curr_open,$paras);
  539.     }
  540.  
  541.  
  542.     # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  543.     #~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  544.     if(grep $_->[1]{'~ignore'}, @$curr_open) {
  545.       DEBUG > 1 and
  546.        print "Skipping $para_type paragraph because in ignore mode.\n";
  547.       next;
  548.     }
  549.     #~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  550.     # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  551.  
  552.     if($para_type eq '=pod') {
  553.       $self->_ponder_pod($para,$curr_open,$paras);
  554.  
  555.     } elsif($para_type eq '=over') {
  556.       next if $self->_ponder_over($para,$curr_open,$paras);
  557.  
  558.     } elsif($para_type eq '=back') {
  559.       next if $self->_ponder_back($para,$curr_open,$paras);
  560.  
  561.     } else {
  562.  
  563.       # All non-magical codes!!!
  564.       
  565.       # Here we start using $para_type for our own twisted purposes, to
  566.       #  mean how it should get treated, not as what the element name
  567.       #  should be.
  568.  
  569.       DEBUG > 1 and print "Pondering non-magical $para_type\n";
  570.  
  571.       my $i;
  572.  
  573.       # Enforce some =headN discipline
  574.       if($para_type =~ m/^=head\d$/s
  575.          and ! $self->{'accept_heads_anywhere'}
  576.          and @$curr_open
  577.          and $curr_open->[-1][0] eq '=over'
  578.       ) {
  579.         DEBUG > 2 and print "'=$para_type' inside an '=over'!\n";
  580.         $self->whine(
  581.           $para->[1]{'start_line'},
  582.           "You forgot a '=back' before '$para_type'"
  583.         );
  584.         unshift @$paras, ['=back', {}, ''], $para;   # close the =over
  585.         next;
  586.       }
  587.  
  588.  
  589.       if($para_type eq '=item') {
  590.  
  591.         my $over;
  592.         unless(@$curr_open and ($over = $curr_open->[-1])->[0] eq '=over') {
  593.           $self->whine(
  594.             $para->[1]{'start_line'},
  595.             "'=item' outside of any '=over'"
  596.           );
  597.           unshift @$paras,
  598.             ['=over', {'start_line' => $para->[1]{'start_line'}}, ''],
  599.             $para
  600.           ;
  601.           next;
  602.         }
  603.         
  604.         
  605.         my $over_type = $over->[1]{'~type'};
  606.         
  607.         if(!$over_type) {
  608.           # Shouldn't happen1
  609.           die "Typeless over in stack, starting at line "
  610.            . $over->[1]{'start_line'};
  611.  
  612.         } elsif($over_type eq 'block') {
  613.           unless($curr_open->[-1][1]{'~bitched_about'}) {
  614.             $curr_open->[-1][1]{'~bitched_about'} = 1;
  615.             $self->whine(
  616.               $curr_open->[-1][1]{'start_line'},
  617.               "You can't have =items (as at line "
  618.               . $para->[1]{'start_line'}
  619.               . ") unless the first thing after the =over is an =item"
  620.             );
  621.           }
  622.           # Just turn it into a paragraph and reconsider it
  623.           $para->[0] = '~Para';
  624.           unshift @$paras, $para;
  625.           next;
  626.  
  627.         } elsif($over_type eq 'text') {
  628.           my $item_type = $self->_get_item_type($para);
  629.             # That kills the content of the item if it's a number or bullet.
  630.           DEBUG and print " Item is of type ", $para->[0], " under $over_type\n";
  631.           
  632.           if($item_type eq 'text') {
  633.             # Nothing special needs doing for 'text'
  634.           } elsif($item_type eq 'number' or $item_type eq 'bullet') {
  635.             die "Unknown item type $item_type"
  636.              unless $item_type eq 'number' or $item_type eq 'bullet';
  637.             # Undo our clobbering:
  638.             push @$para, $para->[1]{'~orig_content'};
  639.             delete $para->[1]{'number'};
  640.              # Only a PROPER item-number element is allowed
  641.              #  to have a number attribute.
  642.           } else {
  643.             die "Unhandled item type $item_type"; # should never happen
  644.           }
  645.           
  646.           # =item-text thingies don't need any assimilation, it seems.
  647.  
  648.         } elsif($over_type eq 'number') {
  649.           my $item_type = $self->_get_item_type($para);
  650.             # That kills the content of the item if it's a number or bullet.
  651.           DEBUG and print " Item is of type ", $para->[0], " under $over_type\n";
  652.           
  653.           my $expected_value = ++ $curr_open->[-1][1]{'~counter'};
  654.           
  655.           if($item_type eq 'bullet') {
  656.             # Hm, it's not numeric.  Correct for this.
  657.             $para->[1]{'number'} = $expected_value;
  658.             $self->whine(
  659.               $para->[1]{'start_line'},
  660.               "Expected '=item $expected_value'"
  661.             );
  662.             push @$para, $para->[1]{'~orig_content'};
  663.               # restore the bullet, blocking the assimilation of next para
  664.  
  665.           } elsif($item_type eq 'text') {
  666.             # Hm, it's not numeric.  Correct for this.
  667.             $para->[1]{'number'} = $expected_value;
  668.             $self->whine(
  669.               $para->[1]{'start_line'},
  670.               "Expected '=item $expected_value'"
  671.             );
  672.             # Text content will still be there and will block next ~Para
  673.  
  674.           } elsif($item_type ne 'number') {
  675.             die "Unknown item type $item_type"; # should never happen
  676.  
  677.           } elsif($expected_value == $para->[1]{'number'}) {
  678.             DEBUG > 1 and print " Numeric item has the expected value of $expected_value\n";
  679.             
  680.           } else {
  681.             DEBUG > 1 and print " Numeric item has ", $para->[1]{'number'},
  682.              " instead of the expected value of $expected_value\n";
  683.             $self->whine(
  684.               $para->[1]{'start_line'},
  685.               "You have '=item " . $para->[1]{'number'} .
  686.               "' instead of the expected '=item $expected_value'"
  687.             );
  688.             $para->[1]{'number'} = $expected_value;  # correcting!!
  689.           }
  690.             
  691.           if(@$para == 2) {
  692.             # For the cases where we /didn't/ push to @$para
  693.             if($paras->[0][0] eq '~Para') {
  694.               DEBUG and print "Assimilating following ~Para content into $over_type item\n";
  695.               push @$para, splice @{shift @$paras},2;
  696.             } else {
  697.               DEBUG and print "Can't assimilate following ", $paras->[0][0], "\n";
  698.               push @$para, '';  # Just so it's not contentless
  699.             }
  700.           }
  701.  
  702.  
  703.         } elsif($over_type eq 'bullet') {
  704.           my $item_type = $self->_get_item_type($para);
  705.             # That kills the content of the item if it's a number or bullet.
  706.           DEBUG and print " Item is of type ", $para->[0], " under $over_type\n";
  707.           
  708.           if($item_type eq 'bullet') {
  709.             # as expected!
  710.  
  711.             if( $para->[1]{'~_freaky_para_hack'} ) {
  712.               DEBUG and print "Accomodating '=item * Foo' tolerance hack.\n";
  713.               push @$para, delete $para->[1]{'~_freaky_para_hack'};
  714.             }
  715.  
  716.           } elsif($item_type eq 'number') {
  717.             $self->whine(
  718.               $para->[1]{'start_line'},
  719.               "Expected '=item *'"
  720.             );
  721.             push @$para, $para->[1]{'~orig_content'};
  722.              # and block assimilation of the next paragraph
  723.             delete $para->[1]{'number'};
  724.              # Only a PROPER item-number element is allowed
  725.              #  to have a number attribute.
  726.           } elsif($item_type eq 'text') {
  727.             $self->whine(
  728.               $para->[1]{'start_line'},
  729.               "Expected '=item *'"
  730.             );
  731.              # But doesn't need processing.  But it'll block assimilation
  732.              #  of the next para.
  733.           } else {
  734.             die "Unhandled item type $item_type"; # should never happen
  735.           }
  736.  
  737.           if(@$para == 2) {
  738.             # For the cases where we /didn't/ push to @$para
  739.             if($paras->[0][0] eq '~Para') {
  740.               DEBUG and print "Assimilating following ~Para content into $over_type item\n";
  741.               push @$para, splice @{shift @$paras},2;
  742.             } else {
  743.               DEBUG and print "Can't assimilate following ", $paras->[0][0], "\n";
  744.               push @$para, '';  # Just so it's not contentless
  745.             }
  746.           }
  747.  
  748.         } else {
  749.           die "Unhandled =over type \"$over_type\"?";
  750.           # Shouldn't happen!
  751.         }
  752.  
  753.         $para_type = 'Plain';
  754.         $para->[0] .= '-' . $over_type;
  755.         # Whew.  Now fall thru and process it.
  756.  
  757.  
  758.       } elsif($para_type eq '=extend') {
  759.         # Well, might as well implement it here.
  760.         $self->_ponder_extend($para);
  761.         next;  # and skip
  762.       } elsif($para_type eq '=encoding') {
  763.         # Not actually acted on here, but we catch errors here.
  764.         $self->_handle_encoding_second_level($para);
  765.  
  766.         next;  # and skip
  767.       } elsif($para_type eq '~Verbatim') {
  768.         $para->[0] = 'Verbatim';
  769.         $para_type = '?Verbatim';
  770.       } elsif($para_type eq '~Para') {
  771.         $para->[0] = 'Para';
  772.         $para_type = '?Plain';
  773.       } elsif($para_type eq 'Data') {
  774.         $para->[0] = 'Data';
  775.         $para_type = '?Data';
  776.       } elsif( $para_type =~ s/^=//s
  777.         and defined( $para_type = $self->{'accept_directives'}{$para_type} )
  778.       ) {
  779.         DEBUG > 1 and print " Pondering known directive ${$para}[0] as $para_type\n";
  780.       } else {
  781.         # An unknown directive!
  782.         DEBUG > 1 and printf "Unhandled directive %s (Handled: %s)\n",
  783.          $para->[0], join(' ', sort keys %{$self->{'accept_directives'}} )
  784.         ;
  785.         $self->whine(
  786.           $para->[1]{'start_line'},
  787.           "Unknown directive: $para->[0]"
  788.         );
  789.  
  790.         # And maybe treat it as text instead of just letting it go?
  791.         next;
  792.       }
  793.  
  794.       if($para_type =~ s/^\?//s) {
  795.         if(! @$curr_open) {  # usual case
  796.           DEBUG and print "Treating $para_type paragraph as such because stack is empty.\n";
  797.         } else {
  798.           my @fors = grep $_->[0] eq '=for', @$curr_open;
  799.           DEBUG > 1 and print "Containing fors: ",
  800.             join(',', map $_->[1]{'target'}, @fors), "\n";
  801.           
  802.           if(! @fors) {
  803.             DEBUG and print "Treating $para_type paragraph as such because stack has no =for's\n";
  804.             
  805.           #} elsif(grep $_->[1]{'~resolve'}, @fors) {
  806.           #} elsif(not grep !$_->[1]{'~resolve'}, @fors) {
  807.           } elsif( $fors[-1][1]{'~resolve'} ) {
  808.             # Look to the immediately containing for
  809.           
  810.             if($para_type eq 'Data') {
  811.               DEBUG and print "Treating Data paragraph as Plain/Verbatim because the containing =for ($fors[-1][1]{'target'}) is a resolver\n";
  812.               $para->[0] = 'Para';
  813.               $para_type = 'Plain';
  814.             } else {
  815.               DEBUG and print "Treating $para_type paragraph as such because the containing =for ($fors[-1][1]{'target'}) is a resolver\n";
  816.             }
  817.           } else {
  818.             DEBUG and print "Treating $para_type paragraph as Data because the containing =for ($fors[-1][1]{'target'}) is a non-resolver\n";
  819.             $para->[0] = $para_type = 'Data';
  820.           }
  821.         }
  822.       }
  823.  
  824.       #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  825.       if($para_type eq 'Plain') {
  826.         $self->_ponder_Plain($para);
  827.       } elsif($para_type eq 'Verbatim') {
  828.         $self->_ponder_Verbatim($para);        
  829.       } elsif($para_type eq 'Data') {
  830.         $self->_ponder_Data($para);
  831.       } else {
  832.         die "\$para type is $para_type -- how did that happen?";
  833.         # Shouldn't happen.
  834.       }
  835.  
  836.       #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  837.       $para->[0] =~ s/^[~=]//s;
  838.  
  839.       DEBUG and print "\n", pretty($para), "\n";
  840.  
  841.       # traverse the treelet (which might well be just one string scalar)
  842.       $self->{'content_seen'} ||= 1;
  843.       $self->_traverse_treelet_bit(@$para);
  844.     }
  845.   }
  846.   
  847.   return;
  848. }
  849.  
  850. ###########################################################################
  851. # The sub-ponderers...
  852.  
  853.  
  854.  
  855. sub _ponder_for {
  856.   my ($self,$para,$curr_open,$paras) = @_;
  857.  
  858.   # Fake it out as a begin/end
  859.   my $target;
  860.  
  861.   if(grep $_->[1]{'~ignore'}, @$curr_open) {
  862.     DEBUG > 1 and print "Ignoring ignorable =for\n";
  863.     return 1;
  864.   }
  865.  
  866.   for(my $i = 2; $i < @$para; ++$i) {
  867.     if($para->[$i] =~ s/^\s*(\S+)\s*//s) {
  868.       $target = $1;
  869.       last;
  870.     }
  871.   }
  872.   unless(defined $target) {
  873.     $self->whine(
  874.       $para->[1]{'start_line'},
  875.       "=for without a target?"
  876.     );
  877.     return 1;
  878.   }
  879.   DEBUG > 1 and
  880.    print "Faking out a =for $target as a =begin $target / =end $target\n";
  881.   
  882.   $para->[0] = 'Data';
  883.   
  884.   unshift @$paras,
  885.     ['=begin',
  886.       {'start_line' => $para->[1]{'start_line'}, '~really' => '=for'},
  887.       $target,
  888.     ],
  889.     $para,
  890.     ['=end',
  891.       {'start_line' => $para->[1]{'start_line'}, '~really' => '=for'},
  892.       $target,
  893.     ],
  894.   ;
  895.   
  896.   return 1;
  897. }
  898.  
  899. sub _ponder_begin {
  900.   my ($self,$para,$curr_open,$paras) = @_;
  901.   my $content = join ' ', splice @$para, 2;
  902.   $content =~ s/^\s+//s;
  903.   $content =~ s/\s+$//s;
  904.   unless(length($content)) {
  905.     $self->whine(
  906.       $para->[1]{'start_line'},
  907.       "=begin without a target?"
  908.     );
  909.     DEBUG and print "Ignoring targetless =begin\n";
  910.     return 1;
  911.   }
  912.   
  913.   unless($content =~ m/^\S+$/s) {  # i.e., unless it's one word
  914.     $self->whine(
  915.       $para->[1]{'start_line'},
  916.       "'=begin' only takes one parameter, not several as in '=begin $content'"
  917.     );
  918.     DEBUG and print "Ignoring unintelligible =begin $content\n";
  919.     return 1;
  920.   }
  921.  
  922.  
  923.   $para->[1]{'target'} = $content;  # without any ':'
  924.  
  925.   $content =~ s/^:!/!:/s;
  926.   my $neg;  # whether this is a negation-match
  927.   $neg = 1        if $content =~ s/^!//s;
  928.   my $to_resolve;  # whether to process formatting codes
  929.   $to_resolve = 1 if $content =~ s/^://s;
  930.   
  931.   my $dont_ignore; # whether this target matches us
  932.   
  933.   foreach my $target_name (
  934.     split(',', $content, -1),
  935.     $neg ? () : '*'
  936.   ) {
  937.     DEBUG > 2 and
  938.      print " Considering whether =begin $content matches $target_name\n";
  939.     next unless $self->{'accept_targets'}{$target_name};
  940.     
  941.     DEBUG > 2 and
  942.      print "  It DOES match the acceptable target $target_name!\n";
  943.     $to_resolve = 1
  944.       if $self->{'accept_targets'}{$target_name} eq 'force_resolve';
  945.     $dont_ignore = 1;
  946.     $para->[1]{'target_matching'} = $target_name;
  947.     last; # stop looking at other target names
  948.   }
  949.  
  950.   if($neg) {
  951.     if( $dont_ignore ) {
  952.       $dont_ignore = '';
  953.       delete $para->[1]{'target_matching'};
  954.       DEBUG > 2 and print " But the leading ! means that this is a NON-match!\n";
  955.     } else {
  956.       $dont_ignore = 1;
  957.       $para->[1]{'target_matching'} = '!';
  958.       DEBUG > 2 and print " But the leading ! means that this IS a match!\n";
  959.     }
  960.   }
  961.  
  962.   $para->[0] = '=for';  # Just what we happen to call these, internally
  963.   $para->[1]{'~really'} ||= '=begin';
  964.   $para->[1]{'~ignore'}   = (! $dont_ignore) || 0;
  965.   $para->[1]{'~resolve'}  = $to_resolve || 0;
  966.  
  967.   DEBUG > 1 and print " Making note to ", $dont_ignore ? 'not ' : '',
  968.     "ignore contents of this region\n";
  969.   DEBUG > 1 and $dont_ignore and print " Making note to treat contents as ",
  970.     ($to_resolve ? 'verbatim/plain' : 'data'), " paragraphs\n";
  971.   DEBUG > 1 and print " (Stack now: ", $self->_dump_curr_open(), ")\n";
  972.  
  973.   push @$curr_open, $para;
  974.   if(!$dont_ignore or scalar grep $_->[1]{'~ignore'}, @$curr_open) {
  975.     DEBUG > 1 and print "Ignoring ignorable =begin\n";
  976.   } else {
  977.     $self->{'content_seen'} ||= 1;
  978.     $self->_handle_element_start((my $scratch='for'), $para->[1]);
  979.   }
  980.  
  981.   return 1;
  982. }
  983.  
  984. sub _ponder_end {
  985.   my ($self,$para,$curr_open,$paras) = @_;
  986.   my $content = join ' ', splice @$para, 2;
  987.   $content =~ s/^\s+//s;
  988.   $content =~ s/\s+$//s;
  989.   DEBUG and print "Ogling '=end $content' directive\n";
  990.   
  991.   unless(length($content)) {
  992.     $self->whine(
  993.       $para->[1]{'start_line'},
  994.       "'=end' without a target?" . (
  995.         ( @$curr_open and $curr_open->[-1][0] eq '=for' )
  996.         ? ( " (Should be \"=end " . $curr_open->[-1][1]{'target'} . '")' )
  997.         : ''
  998.       )
  999.     );
  1000.     DEBUG and print "Ignoring targetless =end\n";
  1001.     return 1;
  1002.   }
  1003.   
  1004.   unless($content =~ m/^\S+$/) {  # i.e., unless it's one word
  1005.     $self->whine(
  1006.       $para->[1]{'start_line'},
  1007.       "'=end $content' is invalid.  (Stack: "
  1008.       . $self->_dump_curr_open() . ')'
  1009.     );
  1010.     DEBUG and print "Ignoring mistargetted =end $content\n";
  1011.     return 1;
  1012.   }
  1013.   
  1014.   unless(@$curr_open and $curr_open->[-1][0] eq '=for') {
  1015.     $self->whine(
  1016.       $para->[1]{'start_line'},
  1017.       "=end $content without matching =begin.  (Stack: "
  1018.       . $self->_dump_curr_open() . ')'
  1019.     );
  1020.     DEBUG and print "Ignoring mistargetted =end $content\n";
  1021.     return 1;
  1022.   }
  1023.   
  1024.   unless($content eq $curr_open->[-1][1]{'target'}) {
  1025.     $self->whine(
  1026.       $para->[1]{'start_line'},
  1027.       "=end $content doesn't match =begin " 
  1028.       . $curr_open->[-1][1]{'target'}
  1029.       . ".  (Stack: "
  1030.       . $self->_dump_curr_open() . ')'
  1031.     );
  1032.     DEBUG and print "Ignoring mistargetted =end $content at line $para->[1]{'start_line'}\n";
  1033.     return 1;
  1034.   }
  1035.  
  1036.   # Else it's okay to close...
  1037.   if(grep $_->[1]{'~ignore'}, @$curr_open) {
  1038.     DEBUG > 1 and print "Not firing any event for this =end $content because in an ignored region\n";
  1039.     # And that may be because of this to-be-closed =for region, or some
  1040.     #  other one, but it doesn't matter.
  1041.   } else {
  1042.     $curr_open->[-1][1]{'start_line'} = $para->[1]{'start_line'};
  1043.       # what's that for?
  1044.     
  1045.     $self->{'content_seen'} ||= 1;
  1046.     $self->_handle_element_end( my $scratch = 'for' );
  1047.   }
  1048.   DEBUG > 1 and print "Popping $curr_open->[-1][0] $curr_open->[-1][1]{'target'} because of =end $content\n";
  1049.   pop @$curr_open;
  1050.  
  1051.   return 1;
  1052.  
  1053. sub _ponder_doc_end {
  1054.   my ($self,$para,$curr_open,$paras) = @_;
  1055.   if(@$curr_open) { # Deal with things left open
  1056.     DEBUG and print "Stack is nonempty at end-document: (",
  1057.       $self->_dump_curr_open(), ")\n";
  1058.       
  1059.     DEBUG > 9 and print "Stack: ", pretty($curr_open), "\n";
  1060.     unshift @$paras, $self->_closers_for_all_curr_open;
  1061.     # Make sure there is exactly one ~end in the parastack, at the end:
  1062.     @$paras = grep $_->[0] ne '~end', @$paras;
  1063.     push @$paras, $para, $para;
  1064.      # We need two -- once for the next cycle where we
  1065.      #  generate errata, and then another to be at the end
  1066.      #  when that loop back around to process the errata.
  1067.     return 1;
  1068.     
  1069.   } else {
  1070.     DEBUG and print "Okay, stack is empty now.\n";
  1071.   }
  1072.   
  1073.   # Try generating errata section, if applicable
  1074.   unless($self->{'~tried_gen_errata'}) {
  1075.     $self->{'~tried_gen_errata'} = 1;
  1076.     my @extras = $self->_gen_errata();
  1077.     if(@extras) {
  1078.       unshift @$paras, @extras;
  1079.       DEBUG and print "Generated errata... relooping...\n";
  1080.       return 1;  # I.e., loop around again to process these fake-o paragraphs
  1081.     }
  1082.   }
  1083.   
  1084.   splice @$paras; # Well, that's that for this paragraph buffer.
  1085.   DEBUG and print "Throwing end-document event.\n";
  1086.  
  1087.   $self->_handle_element_end( my $scratch = 'Document' );
  1088.   return 1; # Hasta la byebye
  1089. }
  1090.  
  1091. sub _ponder_pod {
  1092.   my ($self,$para,$curr_open,$paras) = @_;
  1093.   $self->whine(
  1094.     $para->[1]{'start_line'},
  1095.     "=pod directives shouldn't be over one line long!  Ignoring all "
  1096.      . (@$para - 2) . " lines of content"
  1097.   ) if @$para > 3;
  1098.   # Content is always ignored.
  1099.   return;
  1100. }
  1101.  
  1102. sub _ponder_over {
  1103.   my ($self,$para,$curr_open,$paras) = @_;
  1104.   return 1 unless @$paras;
  1105.   my $list_type;
  1106.  
  1107.   if($paras->[0][0] eq '=item') { # most common case
  1108.     $list_type = $self->_get_initial_item_type($paras->[0]);
  1109.  
  1110.   } elsif($paras->[0][0] eq '=back') {
  1111.     # Ignore empty lists.  TODO: make this an option?
  1112.     shift @$paras;
  1113.     return 1;
  1114.     
  1115.   } elsif($paras->[0][0] eq '~end') {
  1116.     $self->whine(
  1117.       $para->[1]{'start_line'},
  1118.       "=over is the last thing in the document?!"
  1119.     );
  1120.     return 1; # But feh, ignore it.
  1121.   } else {
  1122.     $list_type = 'block';
  1123.   }
  1124.   $para->[1]{'~type'} = $list_type;
  1125.   push @$curr_open, $para;
  1126.    # yes, we reuse the paragraph as a stack item
  1127.   
  1128.   my $content = join ' ', splice @$para, 2;
  1129.   my $overness;
  1130.   if($content =~ m/^\s*$/s) {
  1131.     $para->[1]{'indent'} = 4;
  1132.   } elsif($content =~ m/^\s*((?:\d*\.)?\d+)\s*$/s) {
  1133.     no integer;
  1134.     $para->[1]{'indent'} = $1;
  1135.     if($1 == 0) {
  1136.       $self->whine(
  1137.         $para->[1]{'start_line'},
  1138.         "Can't have a 0 in =over $content"
  1139.       );
  1140.       $para->[1]{'indent'} = 4;
  1141.     }
  1142.   } else {
  1143.     $self->whine(
  1144.       $para->[1]{'start_line'},
  1145.       "=over should be: '=over' or '=over positive_number'"
  1146.     );
  1147.     $para->[1]{'indent'} = 4;
  1148.   }
  1149.   DEBUG > 1 and print "=over found of type $list_type\n";
  1150.   
  1151.   $self->{'content_seen'} ||= 1;
  1152.   $self->_handle_element_start((my $scratch = 'over-' . $list_type), $para->[1]);
  1153.  
  1154.   return;
  1155. }
  1156.       
  1157. sub _ponder_back {
  1158.   my ($self,$para,$curr_open,$paras) = @_;
  1159.   # TODO: fire off </item-number> or </item-bullet> or </item-text> ??
  1160.  
  1161.   my $content = join ' ', splice @$para, 2;
  1162.   if($content =~ m/\S/) {
  1163.     $self->whine(
  1164.       $para->[1]{'start_line'},
  1165.       "=back doesn't take any parameters, but you said =back $content"
  1166.     );
  1167.   }
  1168.  
  1169.   if(@$curr_open and $curr_open->[-1][0] eq '=over') {
  1170.     DEBUG > 1 and print "=back happily closes matching =over\n";
  1171.     # Expected case: we're closing the most recently opened thing
  1172.     #my $over = pop @$curr_open;
  1173.     $self->{'content_seen'} ||= 1;
  1174.     $self->_handle_element_end( my $scratch =
  1175.       'over-' . ( (pop @$curr_open)->[1]{'~type'} )
  1176.     );
  1177.   } else {
  1178.     DEBUG > 1 and print "=back found without a matching =over.  Stack: (",
  1179.         join(', ', map $_->[0], @$curr_open), ").\n";
  1180.     $self->whine(
  1181.       $para->[1]{'start_line'},
  1182.       '=back without =over'
  1183.     );
  1184.     return 1; # and ignore it
  1185.   }
  1186. }
  1187.  
  1188. sub _ponder_item {
  1189.   my ($self,$para,$curr_open,$paras) = @_;
  1190.   my $over;
  1191.   unless(@$curr_open and ($over = $curr_open->[-1])->[0] eq '=over') {
  1192.     $self->whine(
  1193.       $para->[1]{'start_line'},
  1194.       "'=item' outside of any '=over'"
  1195.     );
  1196.     unshift @$paras,
  1197.       ['=over', {'start_line' => $para->[1]{'start_line'}}, ''],
  1198.       $para
  1199.     ;
  1200.     return 1;
  1201.   }
  1202.   
  1203.   
  1204.   my $over_type = $over->[1]{'~type'};
  1205.   
  1206.   if(!$over_type) {
  1207.     # Shouldn't happen1
  1208.     die "Typeless over in stack, starting at line "
  1209.      . $over->[1]{'start_line'};
  1210.  
  1211.   } elsif($over_type eq 'block') {
  1212.     unless($curr_open->[-1][1]{'~bitched_about'}) {
  1213.       $curr_open->[-1][1]{'~bitched_about'} = 1;
  1214.       $self->whine(
  1215.         $curr_open->[-1][1]{'start_line'},
  1216.         "You can't have =items (as at line "
  1217.         . $para->[1]{'start_line'}
  1218.         . ") unless the first thing after the =over is an =item"
  1219.       );
  1220.     }
  1221.     # Just turn it into a paragraph and reconsider it
  1222.     $para->[0] = '~Para';
  1223.     unshift @$paras, $para;
  1224.     return 1;
  1225.  
  1226.   } elsif($over_type eq 'text') {
  1227.     my $item_type = $self->_get_item_type($para);
  1228.       # That kills the content of the item if it's a number or bullet.
  1229.     DEBUG and print " Item is of type ", $para->[0], " under $over_type\n";
  1230.     
  1231.     if($item_type eq 'text') {
  1232.       # Nothing special needs doing for 'text'
  1233.     } elsif($item_type eq 'number' or $item_type eq 'bullet') {
  1234.       die "Unknown item type $item_type"
  1235.        unless $item_type eq 'number' or $item_type eq 'bullet';
  1236.       # Undo our clobbering:
  1237.       push @$para, $para->[1]{'~orig_content'};
  1238.       delete $para->[1]{'number'};
  1239.        # Only a PROPER item-number element is allowed
  1240.        #  to have a number attribute.
  1241.     } else {
  1242.       die "Unhandled item type $item_type"; # should never happen
  1243.     }
  1244.     
  1245.     # =item-text thingies don't need any assimilation, it seems.
  1246.  
  1247.   } elsif($over_type eq 'number') {
  1248.     my $item_type = $self->_get_item_type($para);
  1249.       # That kills the content of the item if it's a number or bullet.
  1250.     DEBUG and print " Item is of type ", $para->[0], " under $over_type\n";
  1251.     
  1252.     my $expected_value = ++ $curr_open->[-1][1]{'~counter'};
  1253.     
  1254.     if($item_type eq 'bullet') {
  1255.       # Hm, it's not numeric.  Correct for this.
  1256.       $para->[1]{'number'} = $expected_value;
  1257.       $self->whine(
  1258.         $para->[1]{'start_line'},
  1259.         "Expected '=item $expected_value'"
  1260.       );
  1261.       push @$para, $para->[1]{'~orig_content'};
  1262.         # restore the bullet, blocking the assimilation of next para
  1263.  
  1264.     } elsif($item_type eq 'text') {
  1265.       # Hm, it's not numeric.  Correct for this.
  1266.       $para->[1]{'number'} = $expected_value;
  1267.       $self->whine(
  1268.         $para->[1]{'start_line'},
  1269.         "Expected '=item $expected_value'"
  1270.       );
  1271.       # Text content will still be there and will block next ~Para
  1272.  
  1273.     } elsif($item_type ne 'number') {
  1274.       die "Unknown item type $item_type"; # should never happen
  1275.  
  1276.     } elsif($expected_value == $para->[1]{'number'}) {
  1277.       DEBUG > 1 and print " Numeric item has the expected value of $expected_value\n";
  1278.       
  1279.     } else {
  1280.       DEBUG > 1 and print " Numeric item has ", $para->[1]{'number'},
  1281.        " instead of the expected value of $expected_value\n";
  1282.       $self->whine(
  1283.         $para->[1]{'start_line'},
  1284.         "You have '=item " . $para->[1]{'number'} .
  1285.         "' instead of the expected '=item $expected_value'"
  1286.       );
  1287.       $para->[1]{'number'} = $expected_value;  # correcting!!
  1288.     }
  1289.       
  1290.     if(@$para == 2) {
  1291.       # For the cases where we /didn't/ push to @$para
  1292.       if($paras->[0][0] eq '~Para') {
  1293.         DEBUG and print "Assimilating following ~Para content into $over_type item\n";
  1294.         push @$para, splice @{shift @$paras},2;
  1295.       } else {
  1296.         DEBUG and print "Can't assimilate following ", $paras->[0][0], "\n";
  1297.         push @$para, '';  # Just so it's not contentless
  1298.       }
  1299.     }
  1300.  
  1301.  
  1302.   } elsif($over_type eq 'bullet') {
  1303.     my $item_type = $self->_get_item_type($para);
  1304.       # That kills the content of the item if it's a number or bullet.
  1305.     DEBUG and print " Item is of type ", $para->[0], " under $over_type\n";
  1306.     
  1307.     if($item_type eq 'bullet') {
  1308.       # as expected!
  1309.  
  1310.       if( $para->[1]{'~_freaky_para_hack'} ) {
  1311.         DEBUG and print "Accomodating '=item * Foo' tolerance hack.\n";
  1312.         push @$para, delete $para->[1]{'~_freaky_para_hack'};
  1313.       }
  1314.  
  1315.     } elsif($item_type eq 'number') {
  1316.       $self->whine(
  1317.         $para->[1]{'start_line'},
  1318.         "Expected '=item *'"
  1319.       );
  1320.       push @$para, $para->[1]{'~orig_content'};
  1321.        # and block assimilation of the next paragraph
  1322.       delete $para->[1]{'number'};
  1323.        # Only a PROPER item-number element is allowed
  1324.        #  to have a number attribute.
  1325.     } elsif($item_type eq 'text') {
  1326.       $self->whine(
  1327.         $para->[1]{'start_line'},
  1328.         "Expected '=item *'"
  1329.       );
  1330.        # But doesn't need processing.  But it'll block assimilation
  1331.        #  of the next para.
  1332.     } else {
  1333.       die "Unhandled item type $item_type"; # should never happen
  1334.     }
  1335.  
  1336.     if(@$para == 2) {
  1337.       # For the cases where we /didn't/ push to @$para
  1338.       if($paras->[0][0] eq '~Para') {
  1339.         DEBUG and print "Assimilating following ~Para content into $over_type item\n";
  1340.         push @$para, splice @{shift @$paras},2;
  1341.       } else {
  1342.         DEBUG and print "Can't assimilate following ", $paras->[0][0], "\n";
  1343.         push @$para, '';  # Just so it's not contentless
  1344.       }
  1345.     }
  1346.  
  1347.   } else {
  1348.     die "Unhandled =over type \"$over_type\"?";
  1349.     # Shouldn't happen!
  1350.   }
  1351.   $para->[0] .= '-' . $over_type;
  1352.  
  1353.   return;
  1354. }
  1355.  
  1356. sub _ponder_Plain {
  1357.   my ($self,$para) = @_;
  1358.   DEBUG and print " giving plain treatment...\n";
  1359.   unless( @$para == 2 or ( @$para == 3 and $para->[2] eq '' )
  1360.     or $para->[1]{'~cooked'}
  1361.   ) {
  1362.     push @$para,
  1363.     @{$self->_make_treelet(
  1364.       join("\n", splice(@$para, 2)),
  1365.       $para->[1]{'start_line'}
  1366.     )};
  1367.   }
  1368.   # Empty paragraphs don't need a treelet for any reason I can see.
  1369.   # And precooked paragraphs already have a treelet.
  1370.   return;
  1371. }
  1372.  
  1373. sub _ponder_Verbatim {
  1374.   my ($self,$para) = @_;
  1375.   DEBUG and print " giving verbatim treatment...\n";
  1376.  
  1377.   $para->[1]{'xml:space'} = 'preserve';
  1378.   for(my $i = 2; $i < @$para; $i++) {
  1379.     foreach my $line ($para->[$i]) { # just for aliasing
  1380.       while( $line =~
  1381.         # Sort of adapted from Text::Tabs -- yes, it's hardwired in that
  1382.         # tabs are at every EIGHTH column.  For portability, it has to be
  1383.         # one setting everywhere, and 8th wins.
  1384.         s/^([^\t]*)(\t+)/$1.(" " x ((length($2)<<3)-(length($1)&7)))/e
  1385.       ) {}
  1386.  
  1387.       # TODO: whinge about (or otherwise treat) unindented or overlong lines
  1388.  
  1389.     }
  1390.   }
  1391.   
  1392.   # Now the VerbatimFormatted hoodoo...
  1393.   if( $self->{'accept_codes'} and
  1394.       $self->{'accept_codes'}{'VerbatimFormatted'}
  1395.   ) {
  1396.     while(@$para > 3 and $para->[-1] !~ m/\S/) { pop @$para }
  1397.      # Kill any number of terminal newlines
  1398.     $self->_verbatim_format($para);
  1399.   } elsif ($self->{'codes_in_verbatim'}) {
  1400.     push @$para,
  1401.     @{$self->_make_treelet(
  1402.       join("\n", splice(@$para, 2)),
  1403.       $para->[1]{'start_line'}, $para->[1]{'xml:space'}
  1404.     )};
  1405.     $para->[-1] =~ s/\n+$//s; # Kill any number of terminal newlines
  1406.   } else {
  1407.     push @$para, join "\n", splice(@$para, 2) if @$para > 3;
  1408.     $para->[-1] =~ s/\n+$//s; # Kill any number of terminal newlines
  1409.   }
  1410.   return;
  1411. }
  1412.  
  1413. sub _ponder_Data {
  1414.   my ($self,$para) = @_;
  1415.   DEBUG and print " giving data treatment...\n";
  1416.   $para->[1]{'xml:space'} = 'preserve';
  1417.   push @$para, join "\n", splice(@$para, 2) if @$para > 3;
  1418.   return;
  1419. }
  1420.  
  1421.  
  1422.  
  1423.  
  1424. ###########################################################################
  1425.  
  1426. sub _traverse_treelet_bit {  # for use only by the routine above
  1427.   my($self, $name) = splice @_,0,2;
  1428.  
  1429.   my $scratch;
  1430.   $self->_handle_element_start(($scratch=$name), shift @_);
  1431.   
  1432.   foreach my $x (@_) {
  1433.     if(ref($x)) {
  1434.       &_traverse_treelet_bit($self, @$x);
  1435.     } else {
  1436.       $self->_handle_text($x);
  1437.     }
  1438.   }
  1439.   
  1440.   $self->_handle_element_end($scratch=$name);
  1441.   return;
  1442. }
  1443.  
  1444. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1445.  
  1446. sub _closers_for_all_curr_open {
  1447.   my $self = $_[0];
  1448.   my @closers;
  1449.   foreach my $still_open (@{  $self->{'curr_open'} || return  }) {
  1450.     my @copy = @$still_open;
  1451.     $copy[1] = {%{ $copy[1] }};
  1452.     #$copy[1]{'start_line'} = -1;
  1453.     if($copy[0] eq '=for') {
  1454.       $copy[0] = '=end';
  1455.     } elsif($copy[0] eq '=over') {
  1456.       $copy[0] = '=back';
  1457.     } else {
  1458.       die "I don't know how to auto-close an open $copy[0] region";
  1459.     }
  1460.  
  1461.     unless( @copy > 2 ) {
  1462.       push @copy, $copy[1]{'target'};
  1463.       $copy[-1] = '' unless defined $copy[-1];
  1464.        # since =over's don't have targets
  1465.     }
  1466.     
  1467.     DEBUG and print "Queuing up fake-o event: ", pretty(\@copy), "\n";
  1468.     unshift @closers, \@copy;
  1469.   }
  1470.   return @closers;
  1471. }
  1472.  
  1473. #--------------------------------------------------------------------------
  1474.  
  1475. sub _verbatim_format {
  1476.   my($it, $p) = @_;
  1477.   
  1478.   my $formatting;
  1479.  
  1480.   for(my $i = 2; $i < @$p; $i++) { # work backwards over the lines
  1481.     DEBUG and print "_verbatim_format appends a newline to $i: $p->[$i]\n";
  1482.     $p->[$i] .= "\n";
  1483.      # Unlike with simple Verbatim blocks, we don't end up just doing
  1484.      # a join("\n", ...) on the contents, so we have to append a
  1485.      # newline to ever line, and then nix the last one later.
  1486.   }
  1487.  
  1488.   if( DEBUG > 4 ) {
  1489.     print "<<\n";
  1490.     for(my $i = $#$p; $i >= 2; $i--) { # work backwards over the lines
  1491.       print "_verbatim_format $i: $p->[$i]";
  1492.     }
  1493.     print ">>\n";
  1494.   }
  1495.  
  1496.   for(my $i = $#$p; $i > 2; $i--) {
  1497.     # work backwards over the lines, except the first (#2)
  1498.     
  1499.     #next unless $p->[$i]   =~ m{^#:([ \^\/\%]*)\n?$}s
  1500.     #        and $p->[$i-1] !~ m{^#:[ \^\/\%]*\n?$}s;
  1501.      # look at a formatty line preceding a nonformatty one
  1502.     DEBUG > 5 and print "Scrutinizing line $i: $$p[$i]\n";
  1503.     if($p->[$i]   =~ m{^#:([ \^\/\%]*)\n?$}s) {
  1504.       DEBUG > 5 and print "  It's a formatty line.  ",
  1505.        "Peeking at previous line ", $i-1, ": $$p[$i-1]: \n";
  1506.       
  1507.       if( $p->[$i-1] =~ m{^#:[ \^\/\%]*\n?$}s ) {
  1508.         DEBUG > 5 and print "  Previous line is formatty!  Skipping this one.\n";
  1509.         next;
  1510.       } else {
  1511.         DEBUG > 5 and print "  Previous line is non-formatty!  Yay!\n";
  1512.       }
  1513.     } else {
  1514.       DEBUG > 5 and print "  It's not a formatty line.  Ignoring\n";
  1515.       next;
  1516.     }
  1517.  
  1518.     # A formatty line has to have #: in the first two columns, and uses
  1519.     # "^" to mean bold, "/" to mean underline, and "%" to mean bold italic.
  1520.     # Example:
  1521.     #   What do you want?  i like pie. [or whatever]
  1522.     # #:^^^^^^^^^^^^^^^^^              /////////////         
  1523.     
  1524.  
  1525.     DEBUG > 4 and print "_verbatim_format considers:\n<$p->[$i-1]>\n<$p->[$i]>\n";
  1526.     
  1527.     $formatting = '  ' . $1;
  1528.     $formatting =~ s/\s+$//s; # nix trailing whitespace
  1529.     unless(length $formatting and $p->[$i-1] =~ m/\S/) { # no-op
  1530.       splice @$p,$i,1; # remove this line
  1531.       $i--; # don't consider next line
  1532.       next;
  1533.     }
  1534.  
  1535.     if( length($formatting) >= length($p->[$i-1]) ) {
  1536.       $formatting = substr($formatting, 0, length($p->[$i-1]) - 1) . ' ';
  1537.     } else {
  1538.       $formatting .= ' ' x (length($p->[$i-1]) - length($formatting));
  1539.     }
  1540.     # Make $formatting and the previous line be exactly the same length,
  1541.     # with $formatting having a " " as the last character.
  1542.  
  1543.     DEBUG > 4 and print "Formatting <$formatting>    on <", $p->[$i-1], ">\n";
  1544.  
  1545.  
  1546.     my @new_line;
  1547.     while( $formatting =~ m{\G(( +)|(\^+)|(\/+)|(\%+))}g ) {
  1548.       #print "Format matches $1\n";
  1549.  
  1550.       if($2) {
  1551.         #print "SKIPPING <$2>\n";
  1552.         push @new_line,
  1553.           substr($p->[$i-1], pos($formatting)-length($1), length($1));
  1554.       } else {
  1555.         #print "SNARING $+\n";
  1556.         push @new_line, [
  1557.           (
  1558.             $3 ? 'VerbatimB'  :
  1559.             $4 ? 'VerbatimI'  :
  1560.             $5 ? 'VerbatimBI' : die("Should never get called")
  1561.           ), {},
  1562.           substr($p->[$i-1], pos($formatting)-length($1), length($1))
  1563.         ];
  1564.         #print "Formatting <$new_line[-1][-1]> as $new_line[-1][0]\n";
  1565.       }
  1566.     }
  1567.     my @nixed =    
  1568.       splice @$p, $i-1, 2, @new_line; # replace myself and the next line
  1569.     DEBUG > 10 and print "Nixed count: ", scalar(@nixed), "\n";
  1570.     
  1571.     DEBUG > 6 and print "New version of the above line is these tokens (",
  1572.       scalar(@new_line), "):",
  1573.       map( ref($_)?"<@$_> ":"<$_>", @new_line ), "\n";
  1574.     $i--; # So the next line we scrutinize is the line before the one
  1575.           #  that we just went and formatted
  1576.   }
  1577.  
  1578.   $p->[0] = 'VerbatimFormatted';
  1579.  
  1580.   # Collapse adjacent text nodes, just for kicks.
  1581.   for( my $i = 2; $i > $#$p; $i++ ) { # work forwards over the tokens except for the last
  1582.     if( !ref($p->[$i]) and !ref($p->[$i + 1]) ) {
  1583.       DEBUG > 5 and print "_verbatim_format merges {$p->[$i]} and {$p->[$i+1]}\n";
  1584.       $p->[$i] .= splice @$p, $i+1, 1; # merge
  1585.       --$i;  # and back up
  1586.     }
  1587.   }
  1588.  
  1589.   # Now look for the last text token, and remove the terminal newline
  1590.   for( my $i = $#$p; $i >= 2; $i-- ) {
  1591.     # work backwards over the tokens, even the first
  1592.     if( !ref($p->[$i]) ) {
  1593.       if($p->[$i] =~ s/\n$//s) {
  1594.         DEBUG > 5 and print "_verbatim_format killed the terminal newline on #$i: {$p->[$i]}, after {$p->[$i-1]}\n";
  1595.       } else {
  1596.         DEBUG > 5 and print
  1597.          "No terminal newline on #$i: {$p->[$i]}, after {$p->[$i-1]} !?\n";
  1598.       }
  1599.       last; # we only want the next one
  1600.     }
  1601.   }
  1602.  
  1603.   return;
  1604. }
  1605.  
  1606.  
  1607. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1608.  
  1609.  
  1610. sub _treelet_from_formatting_codes {
  1611.   # Given a paragraph, returns a treelet.  Full of scary tokenizing code.
  1612.   #  Like [ '~Top', {'start_line' => $start_line},
  1613.   #            "I like ",
  1614.   #            [ 'B', {}, "pie" ],
  1615.   #            "!"
  1616.   #       ]
  1617.   
  1618.   my($self, $para, $start_line, $preserve_space) = @_;
  1619.   
  1620.   my $treelet = ['~Top', {'start_line' => $start_line},];
  1621.   
  1622.   unless ($preserve_space || $self->{'preserve_whitespace'}) {
  1623.     $para =~ s/\.  /\.\xA0 /g if $self->{'fullstop_space_harden'};
  1624.   
  1625.     $para =~ s/\s+/ /g; # collapse and trim all whitespace first.
  1626.     $para =~ s/ $//;
  1627.     $para =~ s/^ //;
  1628.   }
  1629.   
  1630.   # Only apparent problem the above code is that N<<  >> turns into
  1631.   # N<< >>.  But then, word wrapping does that too!  So don't do that!
  1632.   
  1633.   my @stack;
  1634.   my @lineage = ($treelet);
  1635.  
  1636.   DEBUG > 4 and print "Paragraph:\n$para\n\n";
  1637.  
  1638.   # Here begins our frightening tokenizer RE.  The following regex matches
  1639.   # text in four main parts:
  1640.   #
  1641.   #  * Start-codes.  The first alternative matches C< or C<<, the latter
  1642.   #    followed by some whitespace.  $1 will hold the entire start code
  1643.   #    (including any space following a multiple-angle-bracket delimiter),
  1644.   #    and $2 will hold only the additional brackets past the first in a
  1645.   #    multiple-bracket delimiter.  length($2) + 1 will be the number of
  1646.   #    closing brackets we have to find.
  1647.   #
  1648.   #  * Closing brackets.  Match some amount of whitespace followed by
  1649.   #    multiple close brackets.  The logic to see if this closes anything
  1650.   #    is down below.  Note that in order to parse C<<  >> correctly, we
  1651.   #    have to use look-behind (?<=\s\s), since the match of the starting
  1652.   #    code will have consumed the whitespace.
  1653.   #
  1654.   #  * A single closing bracket, to close a simple code like C<>.
  1655.   #
  1656.   #  * Something that isn't a start or end code.  We have to be careful
  1657.   #    about accepting whitespace, since perlpodspec says that any whitespace
  1658.   #    before a multiple-bracket closing delimiter should be ignored.
  1659.   #
  1660.   while($para =~
  1661.     m/\G
  1662.       (?:
  1663.         # Match starting codes, including the whitespace following a
  1664.         # multiple-delimiter start code.  $1 gets the whole start code and
  1665.         # $2 gets all but one of the <s in the multiple-bracket case.
  1666.         ([A-Z]<(?:(<+)\s+)?)
  1667.         |
  1668.         # Match multiple-bracket end codes.  $3 gets the whitespace that
  1669.         # should be discarded before an end bracket but kept in other cases
  1670.         # and $4 gets the end brackets themselves.
  1671.         (\s+|(?<=\s\s))(>{2,})
  1672.         |
  1673.         (\s?>)          # $5: simple end-codes
  1674.         |
  1675.         (               # $6: stuff containing no start-codes or end-codes
  1676.           (?:
  1677.             [^A-Z\s>]
  1678.             |
  1679.             (?:
  1680.               [A-Z](?!<)
  1681.             )
  1682.             |
  1683.             (?:
  1684.               \s(?!\s*>)
  1685.             )
  1686.           )+
  1687.         )
  1688.       )
  1689.     /xgo
  1690.   ) {
  1691.     DEBUG > 4 and print "\nParagraphic tokenstack = (@stack)\n";
  1692.     if(defined $1) {
  1693.       if(defined $2) {
  1694.         DEBUG > 3 and print "Found complex start-text code \"$1\"\n";
  1695.         push @stack, length($2) + 1; 
  1696.           # length of the necessary complex end-code string
  1697.       } else {
  1698.         DEBUG > 3 and print "Found simple start-text code \"$1\"\n";
  1699.         push @stack, 0;  # signal that we're looking for simple
  1700.       }
  1701.       push @lineage, [ substr($1,0,1), {}, ];  # new node object
  1702.       push @{ $lineage[-2] }, $lineage[-1];
  1703.       
  1704.     } elsif(defined $4) {
  1705.       DEBUG > 3 and print "Found apparent complex end-text code \"$3$4\"\n";
  1706.       # This is where it gets messy...
  1707.       if(! @stack) {
  1708.         # We saw " >>>>" but needed nothing.  This is ALL just stuff then.
  1709.         DEBUG > 4 and print " But it's really just stuff.\n";
  1710.         push @{ $lineage[-1] }, $3, $4;
  1711.         next;
  1712.       } elsif(!$stack[-1]) {
  1713.         # We saw " >>>>" but needed only ">".  Back pos up.
  1714.         DEBUG > 4 and print " And that's more than we needed to close simple.\n";
  1715.         push @{ $lineage[-1] }, $3; # That was a for-real space, too.
  1716.         pos($para) = pos($para) - length($4) + 1;
  1717.       } elsif($stack[-1] == length($4)) {
  1718.         # We found " >>>>", and it was exactly what we needed.  Commonest case.
  1719.         DEBUG > 4 and print " And that's exactly what we needed to close complex.\n";
  1720.       } elsif($stack[-1] < length($4)) {
  1721.         # We saw " >>>>" but needed only " >>".  Back pos up.
  1722.         DEBUG > 4 and print " And that's more than we needed to close complex.\n";
  1723.         pos($para) = pos($para) - length($4) + $stack[-1];
  1724.       } else {
  1725.         # We saw " >>>>" but needed " >>>>>>".  So this is all just stuff!
  1726.         DEBUG > 4 and print " But it's really just stuff, because we needed more.\n";
  1727.         push @{ $lineage[-1] }, $3, $4;
  1728.         next;
  1729.       }
  1730.       #print "\nHOOBOY ", scalar(@{$lineage[-1]}), "!!!\n";
  1731.  
  1732.       push @{ $lineage[-1] }, '' if 2 == @{ $lineage[-1] };
  1733.       # Keep the element from being childless
  1734.       
  1735.       pop @stack;
  1736.       pop @lineage;
  1737.       
  1738.     } elsif(defined $5) {
  1739.       DEBUG > 3 and print "Found apparent simple end-text code \"$4\"\n";
  1740.  
  1741.       if(@stack and ! $stack[-1]) {
  1742.         # We're indeed expecting a simple end-code
  1743.         DEBUG > 4 and print " It's indeed an end-code.\n";
  1744.  
  1745.         if(length($5) == 2) { # There was a space there: " >"
  1746.           push @{ $lineage[-1] }, ' ';
  1747.         } elsif( 2 == @{ $lineage[-1] } ) { # Closing a childless element
  1748.           push @{ $lineage[-1] }, ''; # keep it from being really childless
  1749.         }
  1750.  
  1751.         pop @stack;
  1752.         pop @lineage;
  1753.       } else {
  1754.         DEBUG > 4 and print " It's just stuff.\n";
  1755.         push @{ $lineage[-1] }, $5;
  1756.       }
  1757.  
  1758.     } elsif(defined $6) {
  1759.       DEBUG > 3 and print "Found stuff \"$6\"\n";
  1760.       push @{ $lineage[-1] }, $6;
  1761.       
  1762.     } else {
  1763.       # should never ever ever ever happen
  1764.       DEBUG and print "AYYAYAAAAA at line ", __LINE__, "\n";
  1765.       die "SPORK 512512!";
  1766.     }
  1767.   }
  1768.  
  1769.   if(@stack) { # Uhoh, some sequences weren't closed.
  1770.     my $x= "...";
  1771.     while(@stack) {
  1772.       push @{ $lineage[-1] }, '' if 2 == @{ $lineage[-1] };
  1773.       # Hmmmmm!
  1774.  
  1775.       my $code         = (pop @lineage)->[0];
  1776.       my $ender_length =  pop @stack;
  1777.       if($ender_length) {
  1778.         --$ender_length;
  1779.         $x = $code . ("<" x $ender_length) . " $x " . (">" x $ender_length);
  1780.       } else {
  1781.         $x = $code . "<$x>";
  1782.       }
  1783.     }
  1784.     DEBUG > 1 and print "Unterminated $x sequence\n";
  1785.     $self->whine($start_line,
  1786.       "Unterminated $x sequence",
  1787.     );
  1788.   }
  1789.   
  1790.   return $treelet;
  1791. }
  1792.  
  1793. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1794.  
  1795. sub text_content_of_treelet {  # method: $parser->text_content_of_treelet($lol)
  1796.   return stringify_lol($_[1]);
  1797. }
  1798.  
  1799. sub stringify_lol {  # function: stringify_lol($lol)
  1800.   my $string_form = '';
  1801.   _stringify_lol( $_[0] => \$string_form );
  1802.   return $string_form;
  1803. }
  1804.  
  1805. sub _stringify_lol {  # the real recursor
  1806.   my($lol, $to) = @_;
  1807.   use UNIVERSAL ();
  1808.   for(my $i = 2; $i < @$lol; ++$i) {
  1809.     if( ref($lol->[$i] || '') and UNIVERSAL::isa($lol->[$i], 'ARRAY') ) {
  1810.       _stringify_lol( $lol->[$i], $to);  # recurse!
  1811.     } else {
  1812.       $$to .= $lol->[$i];
  1813.     }
  1814.   }
  1815.   return;
  1816. }
  1817.  
  1818. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1819.  
  1820. sub _dump_curr_open { # return a string representation of the stack
  1821.   my $curr_open = $_[0]{'curr_open'};
  1822.  
  1823.   return '[empty]' unless @$curr_open;
  1824.   return join '; ',
  1825.     map {;
  1826.            ($_->[0] eq '=for')
  1827.              ? ( ($_->[1]{'~really'} || '=over')
  1828.                . ' ' . $_->[1]{'target'})
  1829.              : $_->[0]
  1830.         }
  1831.     @$curr_open
  1832.   ;
  1833. }
  1834.  
  1835. ###########################################################################
  1836. my %pretty_form = (
  1837.   "\a" => '\a', # ding!
  1838.   "\b" => '\b', # BS
  1839.   "\e" => '\e', # ESC
  1840.   "\f" => '\f', # FF
  1841.   "\t" => '\t', # tab
  1842.   "\cm" => '\cm',
  1843.   "\cj" => '\cj',
  1844.   "\n" => '\n', # probably overrides one of either \cm or \cj
  1845.   '"' => '\"',
  1846.   '\\' => '\\\\',
  1847.   '$' => '\\$',
  1848.   '@' => '\\@',
  1849.   '%' => '\\%',
  1850.   '#' => '\\#',
  1851. );
  1852.  
  1853. sub pretty { # adopted from Class::Classless
  1854.   # Not the most brilliant routine, but passable.
  1855.   # Don't give it a cyclic data structure!
  1856.   my @stuff = @_; # copy
  1857.   my $x;
  1858.   my $out =
  1859.     # join ",\n" .
  1860.     join ", ",
  1861.     map {;
  1862.     if(!defined($_)) {
  1863.       "undef";
  1864.     } elsif(ref($_) eq 'ARRAY' or ref($_) eq 'Pod::Simple::LinkSection') {
  1865.       $x = "[ " . pretty(@$_) . " ]" ;
  1866.       $x;
  1867.     } elsif(ref($_) eq 'SCALAR') {
  1868.       $x = "\\" . pretty($$_) ;
  1869.       $x;
  1870.     } elsif(ref($_) eq 'HASH') {
  1871.       my $hr = $_;
  1872.       $x = "{" . join(", ",
  1873.         map(pretty($_) . '=>' . pretty($hr->{$_}),
  1874.             sort keys %$hr ) ) . "}" ;
  1875.       $x;
  1876.     } elsif(!length($_)) { q{''} # empty string
  1877.     } elsif(
  1878.       $_ eq '0' # very common case
  1879.       or(
  1880.          m/^-?(?:[123456789]\d*|0)(?:\.\d+)?$/s
  1881.          and $_ ne '-0' # the strange case that that RE lets thru
  1882.       )
  1883.     ) { $_;
  1884.     } else {
  1885.       if( chr(65) eq 'A' ) {
  1886.         s<([^\x20\x21\x23\x27-\x3F\x41-\x5B\x5D-\x7E])>
  1887.          #<$pretty_form{$1} || '\\x'.(unpack("H2",$1))>eg;
  1888.          <$pretty_form{$1} || '\\x{'.sprintf("%x", ord($1)).'}'>eg;
  1889.       } else {
  1890.         # We're in some crazy non-ASCII world!
  1891.         s<([^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789])>
  1892.          #<$pretty_form{$1} || '\\x'.(unpack("H2",$1))>eg;
  1893.          <$pretty_form{$1} || '\\x{'.sprintf("%x", ord($1)).'}'>eg;
  1894.       }
  1895.       qq{"$_"};
  1896.     }
  1897.   } @stuff;
  1898.   # $out =~ s/\n */ /g if length($out) < 75;
  1899.   return $out;
  1900. }
  1901.  
  1902. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1903.  
  1904. # A rather unsubtle method of blowing away all the state information
  1905. # from a parser object so it can be reused. Provided as a utility for
  1906. # backward compatibilty in Pod::Man, etc. but not recommended for
  1907. # general use.
  1908.  
  1909. sub reinit {
  1910.   my $self = shift;
  1911.   foreach (qw(source_dead source_filename doc_has_started
  1912. start_of_pod_block content_seen last_was_blank paras curr_open
  1913. line_count pod_para_count in_pod ~tried_gen_errata errata errors_seen
  1914. Title)) {
  1915.  
  1916.     delete $self->{$_};
  1917.   }
  1918. }
  1919.  
  1920. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  1921. 1;
  1922.  
  1923.